import Vue from 'vue'
import VueRouter from 'vue-router'

import store from '../store'


Vue.use(VueRouter)

// 正常加载
// import Home from '../views/Home.vue'
// 按需（懒）加载（vue实现）
// const Home = () => import( /* webpackChunkName: "home" */ '../views/Home.vue')
// 按需（懒）加载（webpack动态导入）
// require.ensure() 是 webpack 特有的，已经被 import() 取代。
// 大家理解其作用即可，参考issues —— https://github.com/wangyupo/vue-vuex-router/issues/1
// const Home = r => require.ensure([], () => r(require('../views/home')), 'home')


// const FriendCircleSend = () => import( /* webpackChunkName: "friend-circle" */ '../views/FriendCircleSend.vue')
// const FriendCircleMy = () => import( /* webpackChunkName: "friend-circle" */ '../views/FriendCircleMy.vue')
// const FriendCircleLog = () => import( /* webpackChunkName: "friend-circle" */ '../views/FriendCircleLog.vue')

/**
 * meta 可配置参数
 * @param {boolean} icon 页面icon
 * @param {boolean} keepAlive 是否缓存页面
 * @param {string} title 页面标题
 */
const routes = [
    {
        path: '/',
        redirect: {
            path: '/conversation'
        }
    },
    // 登陆页面
    {
        path: '/login',
        name: 'login',
        components: {
            login: () => import(/* webpackChunkName: "login" */ '@/views/LoginPage.vue'),
        }
    },
    // 会话的路由
    {
        path: '/conversation',
        // components: () => import(/* webpackChunkName: "conversation" */ '@/views/PageConversation.vue'),
        components: {
            aside: () => import(/* webpackChunkName: "conversation" */ '@/views/AsideConversation.vue'),
            main: () => import(/* webpackChunkName: "conversation" */ '@/views/MainConversation.vue'),
        }
    },
    // 朋友圈的路由
    {
        path: '/friend-circle',
        // components: () => import(/* webpackChunkName: "conversation" */ '@/views/PageFriendCircle.vue'),
        components: {
            aside: () => import(/* webpackChunkName: "friendCircle" */ '@/views/AsideFriendCircle.vue'),
            main: () => import(/* webpackChunkName: "friendCircle" */ '@/views/MainFriendCircle.vue'),
        },
        children: [
            {
                path: '',
                redirect: 'send'
            },
            {
                path: 'send',
                components: {
                    friendCircle: () => import(/* webpackChunkName: "friendCircle" */ '@/components/friendCircle/FriendCircleSend.vue'),
                },
                meta: {
                    title: '发送朋友圈'
                }
            },
            {
                path: 'my',
                components: {
                    friendCircle: () => import(/* webpackChunkName: "friendCircle" */ '@/components/friendCircle//FriendCircleMy.vue'),
                },
                meta: {
                    title: '我的朋友圈'
                }
            },
            {
                path: 'log',
                components: {
                    friendCircle: () => import(/* webpackChunkName: "friendCircle" */ '@/components/friendCircle//FriendCircleLog.vue'),
                },
                meta: {
                    title: '发朋友圈日志'
                }
            }
        ]
    },
    // 群发助手路由
    {
        path: '/group-assistant',
        // components: () => import(/* webpackChunkName: "conversation" */ '@/views/PageGroupAssistant.vue'),
        components: {
            aside: () => import(/* webpackChunkName: "groupAssistant" */ '@/views/AsideGroupAssistant.vue'),
            main: () => import(/* webpackChunkName: "groupAssistant" */ '@/views/MainGroupAssistant.vue'),
        },
        children: [
            {
                path: '',
                redirect: 'send'
            },
            {
                path: 'send',
                components: {
                    groupAssistant: () => import(/* webpackChunkName: "groupAssistant" */ '@/components/groupAssistant/GroupAssistantSend.vue'),
                },
                meta: {
                    title: '群发助手'
                }
            },
            {
                path: 'log',
                components: {
                    groupAssistant: () => import(/* webpackChunkName: "groupAssistant" */ '@/components/groupAssistant//GroupAssistantLog.vue'),
                },
                meta: {
                    title: '群发日志'
                }
            }
        ]
    },
    // 功能大全的路由
    {
        path: '/tools',
        // components: () => import(/* webpackChunkName: "conversation" */ '@/views/PageTools.vue'),
        components: {
            aside: () => import(/* webpackChunkName: "tools" */ '@/views/AsideTools.vue'),
            main: () => import(/* webpackChunkName: "tools" */ '@/views/MainTools.vue'),
        },
        children: [
            {
                path: '',
                redirect: 'new-friend'
            },
            {
                path: 'new-friend',
                components: {
                    tools: () => import(/* webpackChunkName: "tools" */ '@/components/wxTools/ToolsNewFriend.vue'),
                },
                meta: {
                    title: '新的朋友'
                }
            },
            {
                path: 'add-friend',
                components: {
                    tools: () => import(/* webpackChunkName: "tools" */ '@/components/wxTools/ToolsAddFriend.vue'),
                },
                meta: {
                    title: '添加好友'
                }
            },
            {
                path: 'auto-hi',
                components: {
                    tools: () => import(/* webpackChunkName: "tools" */ '@/components/wxTools/ToolsAutoHi.vue'),
                },
                meta: {
                    title: '自动打招呼'
                }
            },
            {
                path: 'qr-code',
                components: {
                    tools: () => import(/* webpackChunkName: "tools" */ '@/components/wxTools/ToolsQrCode.vue'),
                },
                meta: {
                    title: '导出二维码'
                }
            },
            {
                path: 'auto-task',
                components: {
                    tools: () => import(/* webpackChunkName: "tools" */ '@/components/wxTools/ToolsAutoTask.vue'),
                },
                meta: {
                    title: '自动任务'
                }
            },
            {
                path: 'auto-reply',
                components: {
                    tools: () => import(/* webpackChunkName: "tools" */ '@/components/wxTools/ToolsAutoReply.vue'),
                },
                meta: {
                    title: '自动回复'
                }
            },
            {
                path: 'clear-fans',
                components: {
                    tools: () => import(/* webpackChunkName: "tools" */ '@/components/wxTools/ToolsClearFans.vue'),
                },
                meta: {
                    title: '清理僵尸粉'
                }
            },
            {
                path: 'phone-management',
                components: {
                    tools: () => import(/* webpackChunkName: "tools" */ '@/components/wxTools/ToolsPhoneManagement.vue'),
                },
                meta: {
                    title: '手机管理'
                }
            },
        ]
    }
]

const router = new VueRouter({
    base: process.env.BASE_URL,
    mode: 'history',
    routes,
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            return {
                x: 0,
                y: 0
            }
        }
    }
})

// 登陆页面路由 name
// const LOGIN_PAGE_NAME = 'login'

// 全局前置守卫
router.beforeEach((to, from, next) => {
    const token = store.getters.token
    if (token) {
        // 有token
        if (to.path == '/login') {
            next('/conversation');
        } else {
            next()
        }
    } else {
        // 没有token 跳转到登陆页面
        if (to.path == '/login') {
            //如果是登录页面路径，就直接next()
            next();
        } else {
            //不然就跳转到登录；
            next('/login');
        }
    }
})

// 全局解析守卫
router.beforeResolve((to, from, next) => {
    next()
})

// 全局后置钩子
// router.afterEach((to, from) => {
// })

router.onError(e => {
})

export default router
